﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FSLS
{
    public partial class StatusForm : Form
    {
        FSLSServer Server = null;

        private delegate void SetStatusDelegate(int status);
        private delegate void UpdateConnectionsDelegate();

        public StatusForm()
        {
            InitializeComponent();
            Server = new FSLSServer();
            Server.ServerStatusChanged += SetStatus;
            Server.NewClientAccepted += delegate(int clientId) { UpdateConnectionCount(); };
            Server.ClientDisconnected += delegate(int clientId) { UpdateConnectionCount(); };
        }

        private void StatusForm_Load(object sender, EventArgs e)
        {
            SetStatus(0);
        }

        public void SetStatus(int status)
        {
            if (this.InvokeRequired)
                this.Invoke(new SetStatusDelegate(_SetStatus), status);
            else
                _SetStatus(status);
        }

        void _SetStatus(int status)
        {
            String s = lblStatus.Text.Substring(7);

            if (status == 0)
                s = "Offline";
            else if (status == 1)
                s = "Online";

            lblStatus.Text = String.Format("Status: {0}", s);
        }

        public void UpdateConnectionCount()
        {
            if (this.InvokeRequired)
                this.Invoke(new UpdateConnectionsDelegate(_UpdateConnectionCount));
            else
                _UpdateConnectionCount();
        }

        void _UpdateConnectionCount()
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Server.Start();
        }
    }
}
